<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://library/bookstatus" prefix="book" %>
        <html>

        <head>
            <title>借阅请求列表</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta charset="utf-8">
            <link rel="stylesheet" href="${pageContext.request.contextPath}/asserts/css/bootstrap.min.css">
            <link rel="stylesheet" href="${pageContext.request.contextPath}/asserts/css/common.css">
            <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
            <script src="${pageContext.request.contextPath}/asserts/js/bootstrap.min.js"></script>
            <!-- 添加自定义CSS来确保badge颜色应用 -->
            <style>
                .badge-warning {
                    background-color: #ffc107 !important; /* 黄色 */
                }

                .badge-success {
                    background-color: #28a745 !important; /* 绿色 */
                }

                .badge-danger {
                    background-color: #dc3545 !important; /* 红色 */
                }

                .badge-secondary {
                    background-color: #6c757d !important; /* 灰色 */
                }
            </style>
        </head>

        <body>
            <%@ include file="/asserts/common/header.jsp" %>
                <div class="container">
                    <h2 class="text-center">借阅请求列表</h2>
                    <c:if test="${not empty param.message}">
                        <div class="alert alert-info">${param.message}</div>
                    </c:if>
                    <c:if test="${sessionScope.role == 'user'}">
                        <div class="panel panel-default" style="margin-bottom:16px;">
                            <div class="panel-heading">发起借阅</div>
                            <div class="panel-body">
                                <form class="form-inline" method="post"
                                      action="${pageContext.request.contextPath}/borrowRequests/add">
                                    <div class="form-group">
                                        <label for="bookId">书籍ID：</label>
                                        <input type="number" class="form-control" id="bookId" name="bookId"
                                               placeholder="输入书籍ID">
                                    </div>
                                    <button type="submit" class="btn btn-success"
                                            style="margin-left:8px;">提交借阅请求</button>
                                </form>
                            </div>
                        </div>
                    </c:if>

                    <table class="table table-bordered table-hover">
                        <thead>
                            <tr class="info">
                                <th>ID</th>
                                <th>用户名</th>
                                <th>书名</th>
                                <th>状态</th>
                                <th>申请时间</th>
                                <th>审批时间</th>
                                <th>归还时间</th>
                                <th>审批人ID</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach var="r" items="${sessionScope.requests}">
                                <tr>
                                    <td>${r.id}</td>
                                    <td>${userNames[r.userId]}</td>
                                    <td>${bookTitles[r.bookId]}</td>
                                    <td>  <book:bookStatusTag status="${r.status}" /></td>
                                    <td>${r.requestTime}</td>
                                    <td>${empty r.approvedTime ? '未审批':r.approvedTime}</td>
                                    <td>${empty r.returnTime ?'未归还':r.requestTime}</td>
                                    <td>${empty r.approverId ?'未审批':r.approverId}</td>
                                    <td>
                                        <c:choose>
                                            <c:when test="${sessionScope.role == 'admin'}">
                                                <form method="post"
                                                    action="${pageContext.request.contextPath}/borrowRequests/approve"
                                                    style="display:inline;">
                                                    <input type="hidden" name="id" value="${r.id}">
                                                    <button type="submit" class="btn btn-xs btn-primary" <c:if
                                                        test="${r.status ne 'PENDING'}">disabled</c:if>>通过</button>
                                                </form>
                                                <form method="post"
                                                    action="${pageContext.request.contextPath}/borrowRequests/reject"
                                                    style="display:inline;">
                                                    <input type="hidden" name="id" value="${r.id}">
                                                    <button type="submit" class="btn btn-xs btn-warning" <c:if
                                                        test="${r.status ne 'PENDING'}">disabled</c:if>>拒绝</button>
                                                </form>
                                                <form method="post"
                                                    action="${pageContext.request.contextPath}/borrowRequests/delete"
                                                    style="display:inline;">
                                                    <input type="hidden" name="id" value="${r.id}">
                                                    <button type="submit" class="btn btn-xs btn-danger" <c:if
                                                        test="${r.status eq 'APPROVED'}">disabled</c:if>>删除</button>
                                                </form>
                                            </c:when>
                                            <c:otherwise>
                                                <form method="post"
                                                    action="${pageContext.request.contextPath}/borrowRequests/return"
                                                    style="display:inline;">
                                                    <input type="hidden" name="id" value="${r.id}">
                                                    <button type="submit" class="btn btn-xs btn-success" <c:if
                                                        test="${r.status ne 'APPROVED'}">disabled</c:if>>归还</button>
                                                </form>
                                            </c:otherwise>
                                        </c:choose>
                                    </td>
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>
                </div>
                <script src="${pageContext.request.contextPath}/asserts/js/utils/dynamicBG.js"></script>
                <%@ include file="/asserts/common/footer.jsp" %>
        </body>

        </html>